package ran.youling.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

/***
 * NAME:Ran
 * TIME:2024/10/24
 * 时间处理工具类
 */
public class DateUtil {
    //    date转String
    public static String dateAndString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }


    //String转Date
    public static Date stringAndDate(String str) {
        String dateFormat = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
        try {
            return sdf.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e + "String转Date异常");
        }
    }

    //    数据库 dateTime类型转 String格式;常用于dto
    public static String dateTimeAndString(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return simpleDateFormat.format(date);
    }

    //    数据库 localDate转String格式
    public static String localDateAndString(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    //    数据库 localDateTime转String格式
    public static String localDateTimeAndString(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    //date转localdate 常用于时间比较
    public static LocalDate dateAndLocalDate(Date date) {
        ZonedDateTime zonedDateTime = date.toInstant().atZone(ZoneId.systemDefault());
        return zonedDateTime.toLocalDate();
    }

    // 将LocalDateTime转换为LocalDate
    public static LocalDate localDateTimeAndLocalDate(LocalDateTime localDateTime) {
        return localDateTime.toLocalDate();
    }

    public static LocalDate stringAndLocalDateTime(String str) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return LocalDate.parse(str, formatter);
    }


    //   截取redis尾部时间  只支持尾部 2024-11-07格式
    public static LocalDate redisAndLocalDate(String redisTime) {
        return LocalDate.parse(redisTime.substring(redisTime.length() - 10, redisTime.length()));
    }

    //string类型转localdate
    public static LocalDate stringAndLocalDate(String redisTime) {
        return LocalDate.parse(redisTime, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }


    // 泛型方法，用于对列表进行降序排序
//    食用方式:   DateUtil.sortDescending(goldModels, Comparator.comparing(TbGoldModel::getTime));
    public static <T> List<T> listDesc(List<T> list, Comparator<? super T> comparator) {
        return list.stream()
                .sorted(comparator.reversed()) // 使用提供的比较器进行降序排序
                .collect(Collectors.toList()); // 收集结果到新的列表中
    }


//    public static void main(String[] args) {
//        Date date = new Date();
//        System.out.println(DateAndString(date));
//
//    }
}